import requests
import json

class DataManager:
    def __init__(self, api_url):
        self.api_url = api_url

    def get_data(self, page_no, page_size):
        str_param = f"page_no={page_no}&page_size={page_size}"
        json_data = self.send_get(self.api_url, str_param)
        return self.parse_response(json_data)

    def get_url_data(self):
        json_data = self.send_get(self.api_url, "")
        return self.parse_response(json_data)

    def post_data(self, data):
        json_data = self.send_post(self.api_url, data)
        return self.parse_response(json_data)

    def post_data_gpt(self, data):
        json_data = self.send_post(self.api_url, data)
        return self.parse_response_gpt(json_data)

    def send_get(self, url, params):
        response = requests.get(url, params=params)
        return response.text if response.status_code == 200 else None

    def send_post(self, url, data):
        response = requests.post(url, json=data)
        return response.text if response.status_code == 200 else None

    def send_post2(self, data):
        response = requests.post(self.api_url, json=data)
        return response.text if response.status_code == 200 else None

    @staticmethod
    def parse_response(json_data):
        if json_data:
            parsed_data = json.loads(json_data)
            return ResponseData(parsed_data.get('code'), parsed_data.get('data'))
        return None

    @staticmethod
    def parse_response_gpt(json_data):
        if json_data:
            result = json.loads(json_data)
            code = str(result.get("code"))  # 获取 code，并转换为字符串
            if code == "0":
                data = result.get("data")  # 将 data 字符串转换为 Python 字典
                return str(data.get("data"))  # 获取 data 字典中的 data 字段，并转换为字符串
        return None


class MyData:
    pass


class ResponseData:
    def __init__(self, code, data):
        self.code = code
        self.data = data
